class Solution {//leetcode918——环型子数组最大和
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n=nums.size();
        vector<int> f(n+1,0);
        vector<int> g(n+1,0);
        int f_max=INT_MIN,g_min=INT_MAX,sum=0;
        for(int i=1;i<n+1;i++){
            f[i]=max(nums[i-1],f[i-1]+nums[i-1]);
            f_max=max(f_max,f[i]);
            g[i]=min(nums[i-1],g[i-1]+nums[i-1]);
            g_min=min(g_min,g[i]);
            sum+=nums[i-1];
        }
        return sum==g_min?f_max:max(f_max,sum-g_min);
    }
};